home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / UAppleEvents.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  10.0 KB  |  383 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        UAppleEvents.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __UAPPLEEVENTS__
  15. #include "UAppleEvents.h"
  16. #endif
  17.  
  18. #ifndef __RESOURCES__
  19. #include <Resources.h>
  20. #endif
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __BUFFER__
  27. #include "Buffer.h"
  28. #endif
  29.  
  30. #ifndef __STRING__
  31. #include <String.h>
  32. #endif
  33.  
  34. #ifndef __OBJECTLIST__
  35. #include "ObjectList.h"
  36. #endif
  37.  
  38. #ifndef    __NEWDELETE__
  39. #include "NewDelete.h"
  40. #endif
  41.  
  42. /***********************************|****************************************/
  43.  
  44. struct MAEventTableRec
  45. {
  46.     OSType theClass;
  47.     OSType theID;
  48.     long theValue;
  49. };
  50.  
  51. typedef MAEventTableRec* MAEventTablePointer;
  52.  
  53. //--------------------------------------------------------------------------------------------------
  54. // Globals
  55.  
  56. AEIdleUPP gAppleEventIdleProc = NULL;
  57. AEFilterUPP gAppleEventFilterProc = NULL;
  58. AEEventHandlerUPP    gAppleEventsDispatchUPP;
  59.  
  60. //--------------------------------------------------------------------------------------------------
  61. #pragma segment Initialize
  62.  
  63. void InitUAppleEvents(ProcPtr dispatcher)
  64. {
  65.     short numberOfTables = Count1Resources('aedt');// Count the number of 'aedt' resources
  66.     gAppleEventsDispatchUPP = (AEEventHandlerUPP) NewRoutineDescriptor((ProcPtr)dispatcher, uppAEEventHandlerProcInfo, GetCurrentISA());
  67.  
  68.     for (short tableIndex = 1; tableIndex <= numberOfTables; ++tableIndex)
  69.     {
  70.         Handle tableHandle = Get1IndResource('aedt', tableIndex);
  71.         HLock(tableHandle);
  72.         Size tableSize = GetHandleSize(tableHandle);
  73.         short tableElements = (short)(tableSize / sizeof(MAEventTableRec));
  74.         MAEventTablePointer tablePtr = (MAEventTablePointer) * tableHandle;
  75.  
  76.         for (short eventIndex = 1; eventIndex <= tableElements; ++eventIndex)
  77.         {
  78.             // Install a single event handler for all events
  79.             OSErr err = AEInstallEventHandler(tablePtr->theClass, tablePtr->theID, gAppleEventsDispatchUPP, tablePtr->theValue, false);
  80.             ++tablePtr;
  81.         }
  82.  
  83.         ReleaseResource(tableHandle);
  84.     }
  85. }
  86.  
  87. //--------------------------------------------------------------------------------------------------
  88. #pragma segment AppleEvent
  89.  
  90. TAppleEvent::TAppleEvent
  91. (
  92.     const AEEventClass itsEventClass,
  93.     const AEEventID itsEventID,
  94.     const AEAddressDesc& itsAddress,
  95.     long itsSendingMode
  96. )
  97. {
  98.     fSendingMode = itsSendingMode;
  99.     fPriority = kAENormalPriority;
  100.     fTimeoutVal = kAEDefaultTimeout;
  101.     fFreeMessage = true;
  102.     AECreateAppleEvent(itsEventClass, itsEventID, &itsAddress, kAutoGenerateReturnID, kAnyTransactionID, &fMessage);
  103. }
  104.  
  105. //--------------------------------------------------------------------------------------------------
  106.  
  107. TAppleEvent::TAppleEvent(const AppleEvent& theMessage, Boolean freeMessage)
  108. {
  109.     fSendingMode = kAENoReply;
  110.     fPriority = kAENormalPriority;
  111.     fTimeoutVal = kAEDefaultTimeout;
  112.     fMessage = theMessage;
  113.     fFreeMessage = freeMessage;
  114. }
  115.  
  116. //--------------------------------------------------------------------------------------------------
  117.  
  118.  TAppleEvent::~TAppleEvent()
  119. {
  120.     if (fFreeMessage)
  121.         AEDisposeDesc(&fMessage);
  122. }
  123.  
  124. //--------------------------------------------------------------------------------------------------
  125.  
  126. TAppleEvent* TAppleEvent::Send(OSErr& err)
  127. {
  128.     AppleEvent theReply;
  129.     TAppleEvent* result = NULL;
  130.  
  131.     err = AECreateDesc(typeNull, NULL, 0, &theReply);
  132.  
  133.     if (err != noErr)
  134.         return nil;
  135.  
  136.     err = AESend(&fMessage, &theReply, fSendingMode, fPriority, fTimeoutVal, gAppleEventIdleProc, gAppleEventFilterProc);
  137.  
  138.     if (err != noErr)
  139.         return nil;
  140.  
  141.     if (fSendingMode == kAEWaitReply)
  142.     {
  143.         //    If we were waiting for a reply to our AppleEvent, instantiate a
  144.         //    TAppleEvent and initialize it with theReply.
  145.             result = new TAppleEvent(theReply, true);
  146.             
  147.             if (result == nil) 
  148.             {
  149.                 AEDisposeDesc(&theReply);
  150.                 err = mFulErr;
  151.             }
  152.     }
  153.     else
  154.         AEDisposeDesc(&theReply);
  155.  
  156.     return result;
  157. }
  158.  
  159. //--------------------------------------------------------------------------------------------------
  160.  
  161. OSErr TAppleEvent::GetReturnID(long& returnID)
  162. {
  163.     DescType actualType;
  164.     long actualSize;
  165.     return AEGetAttributePtr(&fMessage, keyReturnIDAttr, typeLongInteger, &actualType, (Ptr) & returnID, sizeof(returnID), &actualSize);
  166. }
  167.  
  168. //--------------------------------------------------------------------------------------------------
  169.  
  170. OSErr TAppleEvent::GetTransactionID(long& transactionID)
  171. {
  172.     DescType actualType;
  173.     long actualSize;
  174.     return  AEGetAttributePtr(&fMessage, keyTransactionIDAttr, typeLongInteger, &actualType, (Ptr) & transactionID, sizeof(transactionID), &actualSize);
  175. }
  176.  
  177. //--------------------------------------------------------------------------------------------------
  178.  
  179. OSErr TAppleEvent::ReadShort(const AEKeyword theKey, short& theValue)
  180. {
  181.     DescType actualType;
  182.     long actualSize;
  183.     theValue = 0;
  184.     return AEGetParamPtr(&fMessage, theKey, typeShortInteger, &actualType, (Ptr) & theValue, sizeof(theValue), &actualSize);
  185. }
  186.  
  187. //--------------------------------------------------------------------------------------------------
  188.  
  189. OSErr TAppleEvent::ReadLong(const AEKeyword theKey, long& theValue)
  190. {
  191.     DescType actualType;
  192.     long actualSize;
  193.     theValue = 0;
  194.  
  195.     return AEGetParamPtr(&fMessage, theKey, typeLongInteger, &actualType, (Ptr) & theValue, sizeof(theValue), &actualSize);
  196. }
  197.  
  198. //--------------------------------------------------------------------------------------------------
  199.  
  200. OSErr TAppleEvent::ReadString(const AEKeyword theKey, char* theData)
  201. {
  202.     DescType actualType;
  203.     long actualSize;
  204.  
  205.     OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeChar, &actualType, (Ptr) theData, 255, &actualSize);
  206.  
  207.     if (theErr == noErr)
  208.     {
  209.         if (actualSize > 255)
  210.             actualSize = 255;
  211.  
  212.         theData[actualSize] = 0;
  213.     }
  214.  
  215.     return theErr;
  216. }
  217.  
  218. //--------------------------------------------------------------------------------------------------
  219.  
  220. OSErr TAppleEvent::ReadParameter (const AEKeyword theKey, StringPtr str)
  221. {
  222.     DescType actualType;
  223.     long actualSize;
  224.  
  225.     OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeChar, &actualType, (Ptr) & str[1], 255, &actualSize);
  226.  
  227.     if (theErr == noErr)
  228.         str[0] = (unsigned char) actualSize;
  229.     else
  230.         str[0] = 0;
  231.  
  232.     return theErr;
  233. }
  234.  
  235. /***********************************|****************************************/
  236.  
  237. OSErr TAppleEvent::ReadParameter ( const AEKeyword key, ADataItem& value )
  238. {    
  239.     //    First, get a handle to the data for this AppleEvent.
  240.     AEDesc dataDescriptor;
  241.     
  242.     OSErr result = AEGetParamDesc ( & fMessage, key, typeWildCard, & dataDescriptor );
  243.         
  244.     if ( result == noErr )
  245.     {
  246.         //    Lock down the data.
  247.         HLock ( dataDescriptor.dataHandle );
  248.  
  249.         if ( value.ReadFrom ( * dataDescriptor.dataHandle, GetHandleSize ( dataDescriptor.dataHandle),
  250.                             dataDescriptor.descriptorType ) == GetHandleSize( dataDescriptor.dataHandle ) )
  251.             result = noErr;
  252.         else
  253.             result = memFullErr; 
  254.         
  255.         AEDisposeDesc ( & dataDescriptor );
  256.     }
  257.     return result;
  258. }
  259.  
  260. /***********************************|****************************************/
  261.  
  262. OSErr TAppleEvent::ReadList ( const AEKeyword theKey, const DescType theType, TObjectList& list )
  263. {
  264.     AEDescList theDescList;
  265.     OSErr theErr = AEGetParamDesc(&fMessage, theKey, typeAEList, &theDescList);
  266.     
  267.     if (theErr == noErr)
  268.     {
  269.         char buffer[256];
  270.         AEKeyword theActualKey;
  271.         long items;
  272.         long actualSize;
  273.         DescType theActualType;
  274.  
  275.         theErr = AECountItems(&theDescList,&items);
  276.  
  277.         if (theErr != noErr)
  278.             return theErr;
  279.  
  280.         for (short index = 1; index <= items; ++index)
  281.         {
  282.             theErr = AEGetNthPtr(&theDescList, index, theType, &theActualKey,&theActualType, (Ptr) &buffer,sizeof(buffer),&actualSize);
  283.             
  284.             if (theErr != noErr)
  285.                 return theErr;
  286.  
  287.             list.Append ( new CBuffer ( buffer, actualSize ) );
  288.         }
  289.     }
  290.  
  291.     return theErr;
  292. }
  293.  
  294. /***********************************|****************************************/
  295. /***********************************|****************************************/
  296.  
  297. TAddressDescription::TAddressDescription ():
  298.     TDirectObject (),
  299.     fAdoptData ( true )
  300. {
  301.     fData.descriptorType = '\?\?\?\?';
  302.     fData.dataHandle = nil;
  303. }
  304.  
  305. /***********************************|****************************************/
  306.  
  307. TAddressDescription::TAddressDescription ( DescType type, const void* data, unsigned long length ):
  308.     TDirectObject (),
  309.     fAdoptData ( true )
  310. {
  311.     fData.descriptorType = type;
  312.     fData.dataHandle = ::AllocateHandle ( length );
  313.     
  314.     if ( fData.dataHandle )
  315.         ::BlockMove ( data, *fData.dataHandle, length );
  316. }
  317.  
  318. /***********************************|****************************************/
  319.  
  320. TAddressDescription::TAddressDescription ( const AEDesc& description, Boolean adoptData ):
  321.     TDirectObject (),
  322.     fData ( description ),
  323.     fAdoptData ( adoptData )
  324. {
  325. }
  326.  
  327. /***********************************|****************************************/
  328.  
  329. TAddressDescription::TAddressDescription ( const TAddressDescription& that ):
  330.     TDirectObject ( that ),
  331.     fAdoptData ( true )
  332. {
  333.     fData.descriptorType = that.fData.descriptorType;
  334.     fData.dataHandle = that.fData.dataHandle;
  335.     
  336.     if ( fData.dataHandle )
  337.         ::HandToHand ( &fData.dataHandle );
  338. }
  339.  
  340. /***********************************|****************************************/
  341.  
  342. TAddressDescription::~TAddressDescription ()
  343. {
  344.     if ( fAdoptData && fData.dataHandle )
  345.         ::DeallocateHandle ( fData.dataHandle );
  346. }
  347.  
  348. /***********************************|****************************************/
  349.  
  350. TAddressDescription&
  351. TAddressDescription::operator = ( const TAddressDescription& that )
  352. {
  353.     TDirectObject::operator = ( that );
  354.     
  355.     if ( fAdoptData && fData.dataHandle )
  356.         ::DeallocateHandle ( fData.dataHandle );
  357.  
  358.     fData.descriptorType = that.fData.descriptorType;
  359.     fData.dataHandle = that.fData.dataHandle;
  360.     
  361.     if ( fData.dataHandle )
  362.         ::HandToHand ( &fData.dataHandle );
  363.         
  364.     return *this;
  365. }
  366.  
  367. /***********************************|****************************************/
  368.  
  369. Boolean
  370. TAddressDescription::operator == ( const TAddressDescription& that ) const
  371. {
  372.     return fData.descriptorType == that.fData.descriptorType;
  373. }
  374.  
  375. /***********************************|****************************************/
  376.  
  377. Boolean
  378. TAddressDescription::operator != ( const TAddressDescription& that ) const
  379. {
  380.     return fData.descriptorType != that.fData.descriptorType;
  381. }
  382.  
  383. /***********************************|****************************************/